﻿
using A.M.ETO;
using Dm.filter.rw;
using DotNetCore.CAP;
using L.M.Review.Application.IServices;
using L.M.Review.Application.Services;
using L.M.Review.Domain.DTO;
using L.M.Review.Domain.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Public_Class_Library;
using Yitter.IdGenerator;
using static L.M.Review.Domain.DTO.GetInforWorkDTO;
using static L.M.Review.ErrorCodeEnum.EnumInfor;

namespace L.M.Review.Read.API.Controllers
{
    /// <summary>
    /// 流程节点表
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Process_nodesController : ControllerBase
    {
        private readonly IProcess_nodesServices process_NodesServices;
        private readonly Cross_service_Helper cross_Service_Helper;
        private readonly IRecord_tableServices record_tableServices;
        private readonly ICapPublisher capPublisher;
        public Process_nodesController(IProcess_nodesServices process_NodesServices, Cross_service_Helper cross_Service_Helper, ICapPublisher capPublisher = null, IRecord_tableServices record_tableServices = null)
        {
            this.process_NodesServices = process_NodesServices;
            this.cross_Service_Helper = cross_Service_Helper;
            this.capPublisher = capPublisher;
            this.record_tableServices = record_tableServices;
        }



        /// <summary>
        /// 新增流程
        /// </summary>
        /// <param name="rootobject"></param>
        /// <returns>新增流程</returns>
        [HttpPost]
        public async Task<IActionResult> GetInforWorkFrow(Rootobject rootobject)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                process_NodesServices.BeginTranInfor();
                List<process_nodes> process_Nodes = new List<process_nodes>();

                List<Node> nodes = new List<Node>();
                nodes.AddRange(rootobject.nodes);
                //找出开始
                GetInforWork? oneinfor = rootobject.nodes.Where(c => c.type == "CustomStart").Select(c => new GetInforWork
                {
                    jiedName = c.text.value,
                    Number = c.id,
                    Winfor = c.properties.propSystem.ToString(),
                    Wname = c.properties.propValue,
                    Previous_level = "",
                }).FirstOrDefault();
                oneinfor.Subordinate = rootobject.edges.Where(c => c.sourceNodeId == oneinfor.Number).FirstOrDefault().targetNodeId;
                nodes.Remove(rootobject.nodes.Where(c => c.id == oneinfor.Number).FirstOrDefault());
                var gers = "http://10.31.59.5:8001/api/System_number_tableRead/GetMyNumber?sName=审批系统";
                var ges = await cross_Service_Helper.Get(gers);
                var number = JsonConvert.DeserializeObject<ystem_number_tableDTO>(ges);

                //获取角色信息
                var roleinfor = await cross_Service_Helper.Get("http://localhost:8001/api/Role_listRead/GetTrueRole");
                var role = JsonConvert.DeserializeObject<List<RoleInfor>>(roleinfor);
                var numrole = role.Where(c => c.role_number == oneinfor.Wname).FirstOrDefault();
                var murole = "";
                if (numrole == null)
                {
                    murole = "";
                }
                else
                {
                    murole = numrole.role_name;
                }
                process_nodes process_Nodes1 = new process_nodes();
                process_Nodes1.process_nodes_number = number.system_number_serial;
                process_Nodes1.process_nodes_name = oneinfor.jiedName;
                process_Nodes1.process_nodes_role_number = oneinfor.Wname;
                process_Nodes1.process_nodes_role_name = murole;
                process_Nodes1.upper_node = YitIdHelper.NextId().ToString() + "_1";
                process_Nodes1.lower_node = oneinfor.Subordinate;
                process_Nodes1.creation_name = "管理员";
                process_Nodes1.passe_system_number = oneinfor.Winfor;
                process_Nodes.Add(process_Nodes1);
                while (nodes.Count() != 0)
                {
                    GetInforWork getInforWork = new GetInforWork();
                    var twoobj = process_Nodes[process_Nodes.Count() - 1];
                    var twoinfor = nodes.Where(c => c.id == twoobj.lower_node).Select(c => new GetInforWork
                    {
                        jiedName = c.text.value,
                        Number = c.id,
                        Wname = c.properties.propValue,
                        Winfor = c.properties.propSystem.ToString(),
                        Previous_level = c.id,
                    }).FirstOrDefault();
                    if (rootobject.edges.Where(c => c.sourceNodeId == twoinfor.Number).FirstOrDefault() != null)
                    {
                        twoinfor.Subordinate = rootobject.edges.Where(c => c.sourceNodeId == twoinfor.Number).FirstOrDefault().targetNodeId;
                    }
                    else
                    {
                        twoinfor.Subordinate = "";
                    }
                    //获取角色信息

                    var numrole1 = role.Where(c => c.role_number == twoinfor.Wname).FirstOrDefault();
                    var murole1 = "";
                    if (numrole1 == null)
                    {
                        murole = "";
                    }
                    else
                    {
                        murole = numrole1.role_name;
                    }

                    process_nodes process_Nodes2 = new process_nodes();
                    process_Nodes2.process_nodes_number = YitIdHelper.NextId().ToString();//雪花ID
                    process_Nodes2.process_nodes_name = twoinfor.jiedName;
                    process_Nodes2.process_nodes_role_number = twoinfor.Wname;
                    process_Nodes2.process_nodes_role_name = murole;
                    process_Nodes2.upper_node = twoinfor.Previous_level;
                    process_Nodes2.lower_node = twoinfor.Subordinate;
                    process_Nodes2.creation_name = "管理员";
                    process_Nodes2.passe_system_number = twoinfor.Winfor;
                    process_Nodes.Add(process_Nodes2);
                    nodes.Remove(rootobject.nodes.Where(c => c.id == twoinfor.Number).FirstOrDefault());
                }

                List<process_nodes>? list = process_Nodes;
                var resss = "";
                foreach (var item in list)
                {
                    if(item.passe_system_number != "")
                    {
                        resss = item.passe_system_number;
                    }
                }
                foreach (var item in list)
                {
                    item.passe_system_number = resss;
                }
                await process_NodesServices.AddInfor_Batch_Async(list);
                process_NodesServices.CommitTranInfor();
                //添加记录表
                record_table record_Table = new record_table();
                record_Table.record_table_number = YitIdHelper.NextId().ToString();
                record_Table.process_nodes_number = number.system_number_serial;
                await record_tableServices.AddInfor_Async(record_Table);
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功";
                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "审批系统-" + number.system_number_serial;
                capPublisher.Publish("GetNewNumber", numberName);
                return Ok(apiReturn);
            }
            catch (Exception)
            {
                process_NodesServices.RollbackTranInfor();
                throw;
            }
        }
    }
}
